package com.jsmtr.rms.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.IBaseExaminerService;
import com.jsmtr.rms.utils.BeanTools;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.StringTools;
import com.jsmtr.rms.utils.db.DbUtil;

@Service
public class BaseExaminerService implements IBaseExaminerService {

    @Resource
    private DaoService dao;

    public void setDao(DaoService dao) {
        this.dao = dao;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<BaseExaminerGroup> getGroups() throws Exception {
        return dao.find("from BaseExaminerGroup");
    }

    @Override
    public BaseExaminerGroup getGroup(String id) throws Exception {
        return (BaseExaminerGroup) dao.findUnique("from BaseExaminerGroup where id='" + id + "'");
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<BaseExaminer> getBaseExaminers(String groupid) throws Exception {
        return dao.find("from BaseExaminer b where b.groupid='" + groupid + "' order by b.code");
    }

    @Override
    public Map<String, Object> getBaseExaminers(int page, int perpage, BaseExaminerGroup group) throws Exception {
        StringBuffer queryHQL = new StringBuffer("from BaseExaminerGroup r where 1=1 ");
        StringBuffer countHQL = new StringBuffer("select count(*) from BaseExaminerGroup r where 1=1 ");
        StringBuffer condition = new StringBuffer();
        condition.append(BeanTools.hqlWhere(group, "r"));

        queryHQL.append(condition);
        countHQL.append(condition);

        return DbUtil.pageQuery(page, perpage, queryHQL.toString(), countHQL.toString(), dao);
    }

    @Override
    public Map<String, Object> getUsers(int page, int perpage, String key) throws Exception {
        StringBuffer queryHQL = new StringBuffer("from BaseUser r where r.ustate='" + EnumTools.State.启用.ordinal() + "' ");
        StringBuffer countHQL = new StringBuffer("select count(*) from BaseUser r where r.ustate='" + EnumTools.State.启用.ordinal() + "' ");

        if (StringTools.notEmpty(key)) {
            queryHQL.append(" and (r.ucode like '%" + key + "%' or r.uname like '%" + key + "%' or r.udeptname like '%" + key + "%')");
            countHQL.append(" and (r.ucode like '%" + key + "%' or r.uname like '%" + key + "%' or r.udeptname like '%" + key + "%')");
        }

        return DbUtil.pageQuery(page, perpage, queryHQL.toString(), countHQL.toString(), dao);

    }
}
